<?php

namespace App\Http\Controllers\F;

use App\Models\Evidence;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Apply;
use App\Models\OperationRecord;
use App\Models\FinanceAccount;
use Illuminate\Support\Facades\DB;
use App\Utils\FileUploader;
class ApplyController extends Controller
{
    public function __construct(){

    }

    public function index(Request $request){
        $apply = new Apply();

        $name = $request->input('name');
        $startTime = $request->input('startTime');
        $endTime = $request->input('endTime');
        if($name){
            $users = User::where('name','like','%'.$name.'%')->select('id')->get()->toArray();
            $userArr = [];
            foreach($users as $user){
                $userArr[] = $user['id'];
            }
            $apply = $apply->whereIn('user_id',$userArr);
        }
        if($startTime){
            $apply = $apply->where('created_at','>=',$startTime);
        }
        if($endTime){
            $apply = $apply->where('created_at','<=',$endTime);
        }
        $applies = $apply->orderBy('id','desc')->paginate(15);

        return view('apply.index',['applies'=>$applies]);
    }

    public function agreePay(Request $request){
        $id = $request->input('id');
        $evidences = Evidence::where('apply_id',$id)->get();
        return view('apply.pay',['id'=>$id,'evidences'=>$evidences]);
    }

    public function detail(Request $request){
        $id = $request->input('id');
        $evidences = Evidence::where('apply_id',$id)->get();
        $operation = OperationRecord::where('apply_id',$id)->first();
        return view('apply.detail',['id'=>$id,'evidences'=>$evidences,'operation'=>$operation]);
    }
    public function postEvidence(Request $request){
        $id = $request->input('apply_id');
        $fileUpload = new FileUploader();
        $info = $fileUpload->upload($request,'file','evidence');
        $evidence = new evidence();
        $evidence->apply_id = $id;
        $evidence->cover_url = $info['url'];
        $evidence->operation_id = $request->user()->id;
        if($evidence->save()){
            return $this->getMessage(0,'上传成功',['url'=>$info['url']]);
        }else{
            return $this->getMessage(1,'上传失败');
        }
    }

    public function postAgreePay(Request $request){
        $id = $request->input('id');
        $evidence = Evidence::where('apply_id',$id)->first();
        if(!$evidence){
            return $this->getMessage(1,'未上传凭证');
        }
        DB::beginTransaction();
        try{
            $apply = Apply::where('id',$id)->where('state',0)->first();
            if($apply){
                $apply->state = 1;
                $apply->save();
                $finance = FinanceAccount::where('id',1)->where('user_id',0)->first();
                $finance->total += $apply->money - $apply->real_money;
                $finance->save();

                $operation = new OperationRecord();
                $operation->operation_id = $request->user()->id;
                $operation->apply_id = $apply->id;
                $operation->remark = '同意付款';
                $operation->save();
            }

            DB::commit();
            $this->getSessionInfo($request,'success','打款成功');
            return $this->getMessage(0,'打款成功');
        }catch(\Exception $e){
            DB::rollback();
            $this->getSessionInfo($request,'error','打款失败');
            return $this->getMessage(1,'打款失败');
        }
    }
    public function denyApply(Request $request){
        $apply_id = $request->input("id");
        $remark = $request->input('reason');
        DB::beginTransaction();
        try{
            $apply = Apply::where('id',$apply_id)->first();
            if($apply->state ==1){
                DB::commit();
                return $this->getMessage(1,'已同意付款,不能拒绝');
            }
            if($apply->state == 0){
                $apply->state = 2;
                $apply->save();
                $operation = new OperationRecord();
                $operation->operation_id = $request->user()->id;
                $operation->apply_id = $apply->id;
                $operation->remark = $remark;
                $operation->save();
            }
            DB::commit();
            return $this->getMessage(0,'操作成功');
        }catch(\Exception $e){
            DB::rollback();
            return $this->getMessage(1,'操作失败');
        }
    }
}